package com.sanpu.kxe.business.main.fragment;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.sanpu.kxe.R;
import com.sanpu.kxe.business.main.bean.InviteFriendsOutput;
import com.sanpu.kxe.business.main.contract.MainConfig;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.common.utils.QRCode;
import com.sanpu.kxe.jump.imageloader.JKImageLoader;
import com.sanpu.kxe.utils.ScreenUtil;
import com.sanpu.kxe.utils.ToastUtil;

import butterknife.ButterKnife;

/**
 * Description：邀请好友
 * Created by 薛勇军 on 2019/4/23.
 */

public class InviteFriendsFragment extends Fragment {

    private LinearLayout showLl;

    private FrameLayout shareViewFl;

    private ImageView backgroundIv, bigIv, qrcodeIv;

    public InviteFriendsFragment() {
    }

    public static InviteFriendsFragment getInstance(InviteFriendsOutput friendsOutput) {
        InviteFriendsFragment fragment = new InviteFriendsFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(MainConfig.INTENT_INVITE_FRIENDS, friendsOutput);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.view_invite_friends, container, false);
        backgroundIv = ButterKnife.findById(rootView, R.id.backgroundIv);
        bigIv = ButterKnife.findById(rootView, R.id.bigIv);
        showLl = ButterKnife.findById(rootView, R.id.showLl);
        qrcodeIv = ButterKnife.findById(rootView, R.id.qrcodeIv);
        shareViewFl = ButterKnife.findById(rootView, R.id.shareViewFl);
        Bundle args = getArguments();

        showContentView((InviteFriendsOutput) args.getSerializable(MainConfig.INTENT_INVITE_FRIENDS));
        return rootView;
    }

    /**
     * 显示内容布局
     */
    private void showContentView(InviteFriendsOutput friendsOutput) {
        if (friendsOutput != null) {
            JKImageLoader.getInstance().displayImage(getContext(),
                    UrlConstants.getBaseUrl() + friendsOutput.getBackgroundImagePath(), backgroundIv);
            JKImageLoader.getInstance().displayImage(getContext(),
                    UrlConstants.getBaseUrl() + friendsOutput.getBigImagePath(), bigIv);
            if (!TextUtils.isEmpty(friendsOutput.getInvitationLinkUrl())) {
                showLl.setVisibility(View.VISIBLE);
                String mCodeUrl = friendsOutput.getInvitationLinkUrl();
                Bitmap bitmap = QRCode.createQRCode(mCodeUrl, 500);
                if (bitmap == null) {
                    ToastUtil.showTextViewPrompt("没有二维码");
                } else {
                    qrcodeIv.setImageBitmap(QRCode.resizeImage(bitmap, ScreenUtil.dp2px(50), ScreenUtil.dp2px(50)));
                }
            } else {
                showLl.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 获取分享资源
     */
    public FrameLayout getShareViewFl() {
        return shareViewFl;
    }
}
